<script setup lang="ts">
  import { ref, reactive } from "vue"
  import { getTenantAreaPage, getTenantAreaTree, getTenantAreaDelete } from '@/api/System'
  import { createMessage } from '@/Public-UIlibrary/pb-message/method'
  import areaModal from './components/areaModal.vue'
  let pages = reactive({
    currentPage: 1,
    pageSize: 15,
    total: 0,
  })
  let loading = ref(false)
  const areaModalRef = ref()
  const areaFormRef = ref()
  let areaListData = reactive([

  ])
  // 层级列表
  let areaList = ref([

  ])
  const column = [{
    key: 'areaCode',
    name: '区域编号',
  },
  {
    key: 'areaName',
    name: '区域名称',
  },
  {
    key: 'areaAddress',
    name: '区域地址',

  },
  {
    key: 'parentAreaName',
    name: '上级区域',
  },
  {
    key: 'description',
    name: '备注',
  },
  {
    slot: 'operation',
    name: '操作',
    width: 200,
  }]
  const areaSearchForm = reactive({
    parentTenantAreaId: '',
    areaName: '',
    areaAddress: ''
  })
  let areaTree = ref([])
  const onInit = () => {
    getTenantAreaPageApi()
    getTenantAreaTreeApi()
  }
  // 确认删除
  const onConfirmDelete = ({ areaId }) => {
    getTenantAreaDeleteApi(areaId)

  }
  const onSearch = () => {
    getTenantAreaPageApi()
  }
  // 新增
  const onAddFn = () => {
    areaModalRef.value.onShow()
  }
  // 编辑 
  const onEdit = (row) => {
    console.log(areaModalRef);
    areaModalRef.value.onShow(row)
  }
  // 选中树节点
  const treeNodeClick = ({ curNode }) => {
    console.log(curNode);

    areaSearchForm.parentTenantAreaId = curNode.areaId
    getTenantAreaPageApi()
  }
  const onReset = () => {
    pages.currentPage = 1

    areaFormRef.value.resetFields()
    getTenantAreaPageApi()
  }
  // 修改或添加成功
  const onConfirmModal = () => {
    onInit()
  }
  // 分页器修改
  const onPagesChange = () => {
    getTenantAreaPageApi()
  }
  // 获取层级接口
  const getTenantAreaPageApi = async () => {
    loading.value = true
    let { total, ...arg } = pages
    let res = await getTenantAreaPage(
      {
        ...arg,
        sortingFields: [{
          field: "",
          order: ""
        }],
        ...areaSearchForm
      }
    )
    loading.value = false
    if (res.code == 2000) {
      pages.total = res.data.total
      areaListData = [...res.data.records]

    }
  }
  // 获取层级树接口
  const getTenantAreaTreeApi = async () => {
    let res = await getTenantAreaTree({})
    if (res.code == 2000) {
      areaTree.value = [res.data]
      console.log(areaTree);

      // console.log(areaList)
    }
  }
  //  删除层级
  const getTenantAreaDeleteApi = async (areaId) => {
    let res = await getTenantAreaDelete({
      areaId
    })
    if (res.code == 2000) {
      createMessage({
        message: '删除成功',
        type: 'success'
      })
      onInit()
    }
  }
  onMounted(() => {
    onInit()
  })
</script>

<template>
  <div class="app-container flex flex-col">
    <SearchCard @onSearch="onSearch" @onReset="onReset">
      <el-form :model="areaSearchForm" ref="areaFormRef" label-width="auto" :inline="true">
        <el-form-item label="区域名称" prop="areaName">
          <pb-input v-model="areaSearchForm.areaName" placeholder="请输入区域名称" />
        </el-form-item>

        <el-form-item label="区域地址" prop="areaAddress">
          <pb-input v-model="areaSearchForm.areaAddress" placeholder="请输入区域地址" />
        </el-form-item>
      </el-form>
    </SearchCard>
    <pb-card padding="0px" class=" flex-1">
      <div v-hasPerm="'center:area:add'">
        <ContentOperation>
          <pb-button size="small" plain @click="onAddFn">+添加</pb-button>
        </ContentOperation>
      </div>

      <div class="flex">
        <ContentLeftTree :treeData="areaTree" label="areaName" @nodeClick="treeNodeClick"></ContentLeftTree>
        <pb-card width="84%">

          <pb-table :tableData='areaListData' :loading="loading" :column='column' :bottomElement="['pb-page']">
            <template v-slot:operation="scope">
              <div class="flex">
                <div v-hasPerm="'center:area:edit'">
                  <pb-button text size="small" @click="onEdit(scope.row)">编辑</pb-button>
                  <p class="divider mr-2 ml-2"></p>
                </div>
                <div v-hasPerm="'center:area:delete'">
                  <PopconfirmButton @confirm="onConfirmDelete(scope.row)"></PopconfirmButton>
                </div>

              </div>
            </template>
          </pb-table>
          <pb-pagination v-model:currentPage="pages.currentPage" v-model:pageSize="pages.pageSize"
            @onChange="onPagesChange" :total="pages.total"></pb-pagination>
        </pb-card>
      </div>
    </pb-card>
    <areaModal ref="areaModalRef" :areaTree="areaTree" @onConfirmModal="onConfirmModal"></areaModal>
  </div>
</template>

<style lang="less" scoped></style>